package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetBrokenInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Asset broken fragment.
 *
 * @author sHadowLess
 */
public class AssetBrokenFragment extends BaseFragment {

    /**
     * The Broken order no.
     */
    @BindView(R.id.broken_orderNo)
    TextView brokenOrderNo;
    /**
     * The Broken make time.
     */
    @BindView(R.id.broken_makeTime)
    TextView brokenMakeTime;
    /**
     * The Broken maker.
     */
    @BindView(R.id.broken_maker)
    TextView brokenMaker;
    /**
     * The Broken apply depart.
     */
    @BindView(R.id.broken_applyDepart)
    TextView brokenApplyDepart;
    /**
     * The Broken broken reason.
     */
    @BindView(R.id.broken_brokenReason)
    TextView brokenBrokenReason;
    /**
     * The Broken info item.
     */
    @BindView(R.id.broken_info_item)
    LinearLayout brokenInfoItem;
    /**
     * The Broken check process.
     */
    @BindView(R.id.broken_check_process)
    LinearLayout brokenCheckProcess;

    private final String url = "api/Scrap/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_broken_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initBrokenInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, brokenCheckProcess);
        }
    }

    private void initBrokenInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getAssetBrokenInfo(orderId, userName, new RequestCallBack<AssetBrokenInfo>() {
                    @Override
                    public void success(RequestResult<AssetBrokenInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                AssetBrokenInfo.DataDTO dataBean = result.getResult().getData();
                                List<AssetBrokenInfo.DataDTO.ItemListDTO> itemList = dataBean.getItemList();

                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String departmentName = dataBean.getDepartmentName();
                                String scrapReason = dataBean.getScrapReason();
                                brokenOrderNo.setText(id);
                                brokenMakeTime.setText(createTimeStr);
                                brokenMaker.setText(createUserName);
                                brokenApplyDepart.setText(departmentName);
                                brokenBrokenReason.setText(scrapReason);


                                for (AssetBrokenInfo.DataDTO.ItemListDTO itemListDTO : itemList) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_asset_broken_item, null, false);
                                    LinearLayout mainTab = view.findViewById(R.id.broken_item);
                                    TextView brokenAssetId = view.findViewById(R.id.broken_assetId);
                                    TextView brokenAssetName = view.findViewById(R.id.broken_assetName);
                                    TextView brokenModel = view.findViewById(R.id.broken_model);
                                    TextView brokenUseDate = view.findViewById(R.id.broken_useDate);
                                    TextView brokenOriginCost = view.findViewById(R.id.broken_originCost);
                                    TextView brokenCutYear = view.findViewById(R.id.broken_cutYear);
                                    TextView brokenMonthCutValue = view.findViewById(R.id.broken_monthCutValue);
                                    TextView brokenAddCutValue = view.findViewById(R.id.broken_addCutValue);
                                    TextView brokenOnlyValue = view.findViewById(R.id.broken_onlyValue);
                                    TextView brokenRemark = view.findViewById(R.id.broken_remark);

                                    String fixedAssetId = itemListDTO.getFixedAssetId();
                                    AssetBrokenInfo.DataDTO.ItemListDTO.AssetObjectDTO assetObjectDTO = itemListDTO.getAssetObject();
                                    String assetName = assetObjectDTO.getAssetName();
                                    String modelNumber = assetObjectDTO.getModelNumber();
                                    String startDateStr = assetObjectDTO.getStartDateStr();
                                    String originalValue = getDecimal(assetObjectDTO.getOriginalValue().doubleValue());
                                    String depreciationYear = getDecimal(itemListDTO.getDepreciationYear().doubleValue());
                                    String depreciationMonth = getDecimal(itemListDTO.getDepreciationMonth().doubleValue());
                                    String depreciationTotal = getDecimal(itemListDTO.getDepreciationTotal().doubleValue());
                                    String leftValue = getDecimal(itemListDTO.getLeftValue().doubleValue());
                                    String remark = itemListDTO.getRemark();

                                    brokenAssetId.setText(fixedAssetId);
                                    brokenAssetName.setText(assetName);
                                    brokenModel.setText(modelNumber);
                                    brokenUseDate.setText(startDateStr);
                                    brokenOriginCost.setText(originalValue);
                                    brokenCutYear.setText(depreciationYear);
                                    brokenMonthCutValue.setText(depreciationMonth);
                                    brokenAddCutValue.setText(depreciationTotal);
                                    brokenOnlyValue.setText(leftValue);
                                    brokenRemark.setText(remark);
                                    brokenInfoItem.addView(mainTab);
                                }
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
